﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;

namespace monitor_2_DBwork
{
    public class DeviceTreeItem
    {
        public string Header { get; set; }
        public object AdditionalInfo { get; set; }
        public void AddSubItem(DeviceTreeItem item)
        {
            if (SubItems == null)
            {
                SubItems = new List<DeviceTreeItem>();
            }
            SubItems.Add(item);
        }

        public List<DeviceTreeItem> SubItems { get; private set; }

        public override string ToString()
        {
            return Header;
        }

        //Info Classes
        public class RegionInfo
        {
            public string Region { get; set; }

            public string MainStation { get; set; }

            public override string ToString()
            {
                return Region;
            }
        }

        public class StationInfo
        {
            private string _coordinates = string.Empty;

            public string BLR { get; set; } 
            public string Station { get; set; }
            public string Coordinates 
            { 
                get { return _coordinates; }
                set
                {
                    if (value == null || value.Length > 14)
                        return;
                    _coordinates = value;
                } 
            }
            public override string ToString()
            {
                return Station;
            }
            public override bool Equals(object obj)
            {
                var another = obj as StationInfo;
                if (another == null)
                    return false;
                return BLR == another.BLR && Station == another.Station && Coordinates == another.Coordinates;
            }
            public StationInfo GetInfo
            {
                get
                {
                    return new StationInfo
                        {
                            BLR = BLR,
                            Station = Station,
                            _coordinates = _coordinates
                        };
                }
            }
        }

        public class DeviceInfo
        {
            private string _ipAddress;

            public string Device { get; set; }
            public string Title { get; set; }
            public bool IsActive { get; set; }
            public string IpAddress
            {
                get { return _ipAddress; }
                set
                {
                    IPAddress ip;
                    const string mask = "[0-9]{0,3}\\.[0-9]{0,3}\\.[0-9]{0,3}\\.[0-9]{0,3}";
                    if (IPAddress.TryParse(value, out ip) && Regex.IsMatch(value, mask))
                    {
                        _ipAddress = value;
                    }
                }
            }
            
            public override string ToString()
            {
                return Device;
            }
        }

        public class RegionNotFoundException : Exception
        {
            public RegionNotFoundException(string message) : base(message)
            {
            }

            public RegionNotFoundException()
            {
                
            }
        }
        //-----------
    }


}
